import { AccessToken } from '@/modules/User/entities/accessToken.entity';
import { BaseEntity } from '@/modules/core/base/BaseEntity';
import { OrderEntity } from '@/modules/shop/entities';
import { CartsEntity } from '@/modules/shop/entities/cart.entity';
import { Exclude, Expose, Type } from 'class-transformer';
import {
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  Entity,
  OneToMany,
  DeleteDateColumn,
} from 'typeorm';

@Entity('user')
@Exclude()
export class UserEntity extends BaseEntity {
  @Expose()
  @Column({ comment: '用户名' })
  username!: string;

  @Column({ comment: '密码' })
  password!: string;

  @Expose()
  @Column({ comment: '邮箱', nullable: true })
  email?: string;

  @Expose()
  @Column({ comment: '电话', nullable: true })
  phone: string;

  @OneToMany(
    (type) => AccessToken,
    (accessToken: AccessToken) => accessToken.user,
    {
      cascade: true,
      onDelete: 'CASCADE',
    },
  )
  accessToken: AccessToken[];

  @OneToMany((type) => CartsEntity, (carts: CartsEntity) => carts.user, {
    onDelete: 'CASCADE',
    cascade: true,
  })
  carts: CartsEntity[];

  @OneToMany((type) => OrderEntity, (order) => order.user, {
    onDelete: 'CASCADE',
    cascade: true,
  })
  order: OrderEntity[];

  @DeleteDateColumn({
    comment: '删除时间',
  })
  @Type(() => Date)
  deleteAt: Date;
}
